package com.marchsoft.campushelp.service.impl;

import com.marchsoft.campushelp.domain.CampusExchangeForm;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import com.marchsoft.campushelp.repository.CampusExchangeFormRepository;
import com.marchsoft.campushelp.service.CampusExchangeFormService;
import com.marchsoft.campushelp.service.dto.CampusExchangeFormDto;
import com.marchsoft.campushelp.service.dto.CampusExchangeFormQueryCriteria;
import com.marchsoft.campushelp.service.mapper.CampusExchangeFormMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author 李诗莹
* @date 2020-03-13
*/
@Service
//@CacheConfig(cacheNames = "campusExchangeForm")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CampusExchangeFormServiceImpl implements CampusExchangeFormService {

    private final CampusExchangeFormRepository campusExchangeFormRepository;

    private final CampusExchangeFormMapper campusExchangeFormMapper;

    public CampusExchangeFormServiceImpl(CampusExchangeFormRepository campusExchangeFormRepository, CampusExchangeFormMapper campusExchangeFormMapper) {
        this.campusExchangeFormRepository = campusExchangeFormRepository;
        this.campusExchangeFormMapper = campusExchangeFormMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(CampusExchangeFormQueryCriteria criteria, Pageable pageable){
        Page<CampusExchangeForm> page = campusExchangeFormRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(campusExchangeFormMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<CampusExchangeFormDto> queryAll(CampusExchangeFormQueryCriteria criteria){
        return campusExchangeFormMapper.toDto(campusExchangeFormRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public CampusExchangeFormDto findById(Long id) {
        CampusExchangeForm campusExchangeForm = campusExchangeFormRepository.findById(id).orElseGet(CampusExchangeForm::new);
        ValidationUtil.isNull(campusExchangeForm.getId(),"CampusExchangeForm","id",id);
        return campusExchangeFormMapper.toDto(campusExchangeForm);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public CampusExchangeFormDto create(CampusExchangeForm resources) {
        return campusExchangeFormMapper.toDto(campusExchangeFormRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(CampusExchangeForm resources) {
        CampusExchangeForm campusExchangeForm = campusExchangeFormRepository.findById(resources.getId()).orElseGet(CampusExchangeForm::new);
        ValidationUtil.isNull( campusExchangeForm.getId(),"CampusExchangeForm","id",resources.getId());
        campusExchangeForm.copy(resources);
        campusExchangeFormRepository.save(campusExchangeForm);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            campusExchangeFormRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<CampusExchangeFormDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CampusExchangeFormDto campusExchangeForm : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("悬赏表id", campusExchangeForm.getRewardTableId());
            map.put("买家id", campusExchangeForm.getBuyersId());
            map.put("交易时间", campusExchangeForm.getTradingTime());
            map.put("0-完成，1-没有，3-撤回（交易状态）", campusExchangeForm.getSate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}